package collision;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import java.util.Timer;
import java.util.TimerTask;


public class Board extends InFrame implements ActionListener {

    private Table table;
    private Timer timer;
    private Pokemon pokemon;
    private ArrayList aliens;
    private Scenery scene;
    private boolean ingame;
    private int B_WIDTH;
    private int B_HEIGHT;
    private int gamestart;
    private int times;
    private ArrayList monsters;
    private int ALIENNUM = 3;
    SelectPokemon sel = new SelectPokemon();


    public Board(int THIS_WIDTH, int THIS_HEIGHT) {
        super.removeAll();
        table = new Table();
        this.gamestart = 0;
        
        addMouseListener(new MAdapter());
        setFocusable(true);
        requestFocusInWindow(true);
        setBackground(Color.BLACK);
       setDoubleBuffered(true);
        ingame = true;

        setSize(THIS_WIDTH, THIS_HEIGHT);

        pokemon = new Pokemon();

        initAliens();
        scene = new Scenery();
        times = 0;
        
        timer = new Timer();
        TimerTasking timertask = new TimerTasking();
        timer.schedule(timertask, 5, 40);
        monsters = new ArrayList();
        
        System.out.print("Entrou na Board");
        setVisible(true);
        
    }

    @Override
    public void addNotify() {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();   
    }

    private void initAliens() {
        aliens = new ArrayList();

        aliens.add(new Zombie(this.B_WIDTH + 1000));
    }

@Override
    public void paint(Graphics g) {
        super.paint(g);

            if (ingame) {

            Graphics2D g2d = (Graphics2D)g;
            
            g2d.drawImage(scene.getImage(), 0, 0, this);
            
            for(int i = 0; i< monsters.size(); i++){
                Pokemon mon = (Pokemon) monsters.get(i);
                if (mon.isVisible())
                    g2d.drawImage(mon.getImage(), mon.getX(), mon.getY(), mon.getX()+ mon.getImagew(), mon.getY() + 30, mon.getImagex1(), 0, mon.getImagex1()+ mon.getImagew(), 30, this);
                //else monsters.remove(mon);

            ArrayList ms = mon.getMissiles();

            for (int j = 0; j < ms.size(); j++) {
                Missile m = (Missile)ms.get(j);
                g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
            }

            }

            for (int i = 0; i < aliens.size(); i++) {
                Zombie a = (Zombie)aliens.get(i);
                if (a.isVisible())
                    g2d.drawImage(a.getImage(), a.getX(), a.getY(), a.getX()+ a.getImagew(), a.getY() + 30, a.getImagex(), 0, a.getImagex() + a.getImagew(), 30, this);
                else aliens.remove(i);
            }

            /*for(int i = 0; i < monsters.size(); i++){
                Craft b = (Craft)monsters.get(i);
                if(b.isVisible())
                    g2d.drawImage(b.getImage(), b.getX(), b.getY(), b.getX()+ b.getImagew(), b.getY() + 30, b.getImagex1(), 0, b.getImagex1()+ b.getImagew(), 30, this);       
            }*/
            
            g2d.setColor(Color.WHITE);
            g2d.drawString("Aliens left: " + aliens.size(), 5, 15);


        } else {
            String msg = "Game Over";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2,
                         B_HEIGHT / 2);
        }
        

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


    @Override
   public void actionPerformed(ActionEvent e) {
        times++;

        if(times%300 == 0)
            aliens.add(new Zombie(this.B_WIDTH+ 200));
        if( times > 1000) times = 0;
        if (aliens.isEmpty()) {
            ingame = false;
        }

      for(int j = 0; j < monsters.size(); j++){
        Pokemon mon = (Pokemon) monsters.get(j);
        mon.move(times);
        ArrayList ms = mon.getMissiles();

        for (int i = 0; i < ms.size(); i++) {
            Missile m = (Missile) ms.get(i);
            if (m.isVisible())
                m.move();
            else ms.remove(i);
        }

      }
        

        for (int i = 0; i < aliens.size(); i++) {
            Zombie a = (Zombie) aliens.get(i);
            if (a.isVisible()){
                if( a.move(times) == false)
                    ingame = false;
            }
            else aliens.remove(i);
        }
        
        checkCollisions(times);
        repaint();  
    }

    private class TimerTasking extends TimerTask{

    public TimerTasking(){

    }

    @Override
        @SuppressWarnings("SizeReplaceableByIsEmpty")
    public void run() {
        times++;

        if(times%300 == 0)
            aliens.add(new Zombie(B_WIDTH+ 200));
        if( times > 1000) times = 0;
        //if (aliens.size() == 0) {
          //  ingame = false;
       // }

      for(int j = 0; j < monsters.size(); j++){
        Pokemon mon = (Pokemon) monsters.get(j);
        mon.move(times);
        ArrayList ms = mon.getMissiles();

        for (int i = 0; i < ms.size(); i++) {
            Missile m = (Missile) ms.get(i);
            if (m.isVisible())
                m.move();
            else ms.remove(i);
        }

      }


        for (int i = 0; i < aliens.size(); i++) {
            Zombie a = (Zombie) aliens.get(i);
            if (a.isVisible()){
                if( a.move(times) == false)
                    ingame = false;
            }
            else aliens.remove(i);
        }
       
        checkCollisions(times);
        repaint();

    }

}
    public void checkCollisions(int times) {
        
        Collision col = new Collision();
        
         for( int k = 0; k < monsters.size(); k++){
            Pokemon mon = (Pokemon) monsters.get(k);

            for (int j = 0; j<aliens.size(); j++) {
                Zombie a = (Zombie) aliens.get(j);
                col.collide(a, mon, times);
        }
        


        ArrayList ms = mon.getMissiles();

        for (int i = 0; i < ms.size(); i++) {
            Missile m = (Missile) ms.get(i);


            for (int j = 0; j<aliens.size(); j++) {
                Zombie a = (Zombie) aliens.get(j);
                col.collide(a, m, times);
                
            }
        }
        }
    }


    
    private class MAdapter extends MouseAdapter{
        
        @Override
        public void mouseClicked(MouseEvent e){
  
            
            int x = sel.findCol(e.getX());
            int y = sel.findRow(e.getY());

            System.out.println("x =" + (e.getX()) +"  y =" + (e.getY()) + "\n");
            System.out.printf("x = %d, y = %d", x, y);
            if( x != -1 && y != -1)
                sel.selecting(e.getX(), e.getY());

             x = table.insertX(e.getX() - 14);
             y = table.insertY(e.getY() - 15);
            if( x != -1 && y != -1)
                monsters.add(sel.getPoke(x,y));



            //craft.mouseClicked(e);
        }
    }
}